// pages/tuibao-integral-mall/charity-detail/charity-detail.js
var WxParse = require("../../../wxParse/wxParse.js"), shoppingCart = require("../../../components/shopping_cart/shopping_cart.js"), specificationsModel = require("../../../components/specifications_model/specifications_model.js"), gSpecificationsModel = require("../../../components/goods/specifications_model.js"), goodsBanner = require("../../../components/goods/goods_banner.js"), goodsInfo = require("../../../components/goods/goods_info.js"), goodsBuy = require("../../../components/goods/goods_buy.js"), goodsRecommend = require("../../../components/goods/goods_recommend.js"), p = 1, is_loading_comment = !1, is_more_comment = !0, share_count = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //点赞图片
    before: "/images/xin-before.jpg",
    after: "/images/xin-after.jpg",
    goods: {},
    fabulous: {
      hasfabulous: true,
      count: 17322
    },
    hide: "hide",
    quick: 0,
    pageType: "charity",
    id: '',
  },

  /**点赞 */
  dianzan: function (e) {
    console.log('点赞=======',e)
    var that = this;
    let fabulous = that.data.fabulous
    fabulous.hasfabulous = true;
    that.setData({
      left: e.changedTouches[0].clientX,
      top: e.changedTouches[0].clientY,
      parse: true,
      fabulous: fabulous,
    })
    //接下来声明动画添加1秒，又快到慢
    var animationMiddleHeaderItem = that.data.animationMiddleHeaderItem;

    animationMiddleHeaderItem = wx.createAnimation({
      duration: 1000, // 以毫秒为单位  
      timingFunction: 'ease-out'
    });
    animationMiddleHeaderItem.opacity(0.6).scale(4, 4).step();
    //opacity动画加个透明度，scale表示放大缩小多少step表示执行完整个动画才执行下一个
    that.setData({
      animationMiddleHeaderItem: animationMiddleHeaderItem.export() //输出动画

    })
    setTimeout(() => {
      that.setData({
        parse: false,
        animationMiddleHeaderItem: null
      })
    }, 1000);
    (wx.getStorageSync('USER_INFO') && wx.getStorageSync('USER_INFO').access_token) || getApp().page.setUserInfoShow();

    this.point_like()
  },
  point_like() {
    var that = this;
    getApp().request({
      url: getApp().api.sign_up.default_lik,
      method: 'post',
      data: {
        id: this.data.goods.id, // 商品编号
        recommend_id: this.data.recommend_id // 推荐人id，可允许为空
      },
      success: function (t) {
        if (t.code == 0) {
          that.setData({
            is_like: 1
          })
        }
        wx.showToast({
          title: t.msg,
          icon: 'none',
          duration: 2500
        })
      },
      complete: function (t) {
        if (t.data.code == -1 && t.data.msg == '请先登陆') {
          wx.setStorageSync('is_like', true)
        }
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log('options',options)
    getApp().page.onLoad(this, options),this.loadData();

    if(options.scene){
      options = decodeURIComponent(options.scene)
      if (void 0 !== options) {
        options = getApp().helper.scene_decode(options);
      }
    }
    if(wx.getStorageSync('ACCESS_TOKEN')){
      console.log('已登录')
      var recommender_id  = wx.getStorageSync('recommender_id')
      console.log('recommender_id==',recommender_id)
      getApp().page.bindParent1(wx.getStorageSync('PARENT_ID'))
      if(options.recommend_id){
        if(recommender_id  === '' || recommender_id === '0'){
          console.log('wu')
          getApp().page.bindParent1(options.recommend_id)
          wx.setStorageSync('recommender_id', options.recommend_id)
        }else{
          console.log('you')
        }
      }
    }else{
      console.log('未登录')
      var recommender_id  = wx.getStorageSync('recommender_id')
      console.log('recommender_id==',recommender_id)
      if(options.recommend_id){
        if(recommender_id  === '' || recommender_id === '0'){
          console.log('wu',options.recommend_id)
          wx.setStorageSync('recommender_id', options.recommend_id)
        }else{
          console.log('you')
        }
      }
  
      if(options.scene){
        options = decodeURIComponent(options.scene)
       var ccc = getApp().helper.scene_decode(options);
        console.log(options,'ccccc++++',ccc)
        if(recommender_id  === '' || recommender_id === '0'){
          console.log('wu')
          wx.setStorageSync('recommender_id', ccc.uid)
        }else{
          console.log('you')
        }
        if (void 0 !== options) {
          options = getApp().helper.scene_decode(options);
        }
      }
    }
    options.title && wx.setNavigationBarTitle({
      title: options.title
    })
    //分享图片aid、uid   转发id、recommend_id
    this.setData({
      id: options.id || options.aid,
      recommend_id: options.recommend_id || options.uid || '',
      title: options.title || ''
    })
    getApp().request({
      url: getApp().api.sign_up.default_detail,
      data: {
        id: this.data.id,
        recommend_id: this.data.recommend_id
      },
      success: (res) => {
        if (res.code == 0) {
          WxParse.wxParse("detail", "html", res.data.activity.content, this);
          this.setData({
            goods: res.data.activity,
            is_like: res.data.is_like,
            setting: res.data.setting,
            like_users_count: res.data.like_users_count,
            like_users: res.data.like_users,
          })
          //授权刷新页面后点赞
          if (wx.getStorageSync('is_like')) {
            wx.removeStorage({
              key: 'is_like',
              success: function (res) { },
            })
            this.point_like()
          }
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  loadData: function () {
    var a = this;
    getApp().core.showLoading({
        title: "正在加载",
        mask: !0
    }), getApp().request({
        url: getApp().api.share.index,
        success: function (e) {
            if (0 == e.code) {
                if (e.data.share_setting) var t = e.data.share_setting; else t = e.data;
                getApp().core.setStorageSync(getApp().const.SHARE_SETTING, t), a.setData({
                    share_setting: t
                });
            }
        },
        complete: function () {
            getApp().core.hideLoading();
        }
    });
},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    getApp().page.onShow(this), shoppingCart.init(this), specificationsModel.init(this, shoppingCart),
    gSpecificationsModel.init(this), goodsBanner.init(this), goodsInfo.init(this), goodsBuy.init(this),
    goodsRecommend.init(this);
    var n = this;

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    getApp().page.onShareAppMessage(this);
    var o = this, t = getApp().getUser();
    return {
      path: "/pages/tuibao-integral-mall/charity-detail/charity-detail?id=" + this.data.goods.id + "&recommend_id=" + t.id + "&title=" + this.data.title,
      success: function (t) {
        1 == ++share_count && o.shareSendCoupon(o);
      },
      title: o.data.goods.name,
      imageUrl: o.data.goods.pic_list && o.data.goods.pic_list[0] ? o.data.goods.pic_list[0] : ''
    };
  }
})